#include "FileCryptoer.h"
#include "Poco\Crypto\Cipher.h"
#include "Poco\Util\Application.h"
#include "Poco\Util\LayeredConfiguration.h"
using Poco::Crypto::Cipher;
using Poco::Util::Application;
FileCryptoer::FileCryptoer() 
{

}


FileCryptoer::~FileCryptoer()
{
}

std::string FileCryptoer::encodeContents(std::string contents, std::string file_base, std::string file_ext)
{
	Application& app = Application::instance();
	std::string fh = app.config().getString(file_ext + ".file_mode.file_header", "");
	if (!fh.empty())
		fh += "\r\n";
	std::string ft = app.config().getString(file_ext + ".file_mode.file_tailer", "");
	return fh+pCipher->encryptString(contents, Cipher::ENC_BASE64)+ ft;
}
std::string FileCryptoer::decodeContents(std::string contents, std::string file_base, std::string file_ext)
{
	Application& app = Application::instance();
	std::string fh = app.config().getString(file_ext + ".file_mode.file_header", "");
	if (!fh.empty())
		fh += "\r\n";
	std::string ft = app.config().getString(file_ext + ".file_mode.file_tailer", "");
	size_t fhlen = fh.length();
	size_t ftlen = ft.length();
	std::string sbuf = contents.substr(fhlen, contents.length() - fhlen - ftlen);
	std::string out = pCipher->decryptString(sbuf, Cipher::ENC_BASE64);
	return out;
}

void FileCryptoer::showInfo()
{
	Application& app = Application::instance();
	app.logger().information("Using file mode.\n");
}
